package com.ruoyi.jwxt.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.jwxt.utility.JwxtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.jwxt.domain.DTO.JwxtSchedulingItemDTO;
import com.ruoyi.jwxt.domain.DTO.JwxtSchedulingQueryParamsDTO;
import com.ruoyi.jwxt.domain.PO.JwxtLab;
import com.ruoyi.jwxt.domain.PO.JwxtLabScheduling;
import com.ruoyi.jwxt.service.IJwxtCourseScheduleService;
import com.ruoyi.jwxt.service.IJwxtLabSchedulingService;
import com.ruoyi.jwxt.service.IJwxtLabService;

/**
 * 课程表Service业务层处理
 */
@Service
public class JwxtCourseScheduleServiceImpl implements IJwxtCourseScheduleService {

    @Autowired
    private IJwxtLabSchedulingService labSchedulingService;

    @Autowired
    private IJwxtLabService labService;

    @Autowired
    private JwxtUtils jwxtUtils;

    private String getItem(JwxtLabScheduling labScheduling) {
        return "课程：" + labScheduling.getCourseName() + "\n" +
                "教师：" + labScheduling.getTeacherName() + "\n" +
                "班级：" + labScheduling.getStudentClass() + "\n" +
                "周次：" + labScheduling.getStartWeek() + "-" + labScheduling.getEndWeek();
    }

    /**
     * 查询课程表
     */
    @Override
    public List<JwxtSchedulingItemDTO> select(JwxtSchedulingQueryParamsDTO schedulingQueryParamsDTO) {

        if (schedulingQueryParamsDTO.getSemester() == null) {
            schedulingQueryParamsDTO.setSemester(jwxtUtils.getCurSemester());
        }

        List<JwxtSchedulingItemDTO> schedulingItemDTOs = new ArrayList<>();

        Long startDay = Long.valueOf(1);
        Long endDay = Long.valueOf(7);
        if (schedulingQueryParamsDTO.getDay() != null) {
            startDay = schedulingQueryParamsDTO.getDay();
            endDay = schedulingQueryParamsDTO.getDay();
        }
        for (Long day = startDay; day <= endDay; day++) {
            JwxtLabScheduling labScheduling = new JwxtLabScheduling();
            labScheduling.setSemester(schedulingQueryParamsDTO.getSemester());
            labScheduling.setDay(day);

            List<Long> ladIds = labSchedulingService.select(labScheduling).stream()
                    .filter(scheduling -> scheduling.getStartWeek() <= schedulingQueryParamsDTO.getWeek()
                            && schedulingQueryParamsDTO.getWeek() <= scheduling.getEndWeek())
                    .map(JwxtLabScheduling::getLabId).distinct()
                    .collect(Collectors.toList());

            for (Long labId : ladIds) {
                labScheduling.setLabId(labId);
                JwxtSchedulingItemDTO schedulingItemDTO = new JwxtSchedulingItemDTO();
                Boolean flag = false;

                labScheduling.setSession("1-2");
                List<JwxtLabScheduling> labSchedulingList = labSchedulingService.select(labScheduling).stream()
                        .filter(scheduling -> scheduling.getStartWeek() <= schedulingQueryParamsDTO.getWeek()
                                && schedulingQueryParamsDTO.getWeek() <= scheduling.getEndWeek())
                        .collect(Collectors.toList());
                if (labSchedulingList.size() > 0) {
                    flag = true;
                    schedulingItemDTO.setInfo_1_2(getItem(labSchedulingList.get(0)));
                }

                labScheduling.setSession("3-5");
                labSchedulingList = labSchedulingService.select(labScheduling).stream()
                        .filter(scheduling -> scheduling.getStartWeek() <= schedulingQueryParamsDTO.getWeek()
                                && schedulingQueryParamsDTO.getWeek() <= scheduling.getEndWeek())
                        .collect(Collectors.toList());
                if (labSchedulingList.size() > 0) {
                    flag = true;
                    schedulingItemDTO.setInfo_3_5(getItem(labSchedulingList.get(0)));
                }

                labScheduling.setSession("6-7");
                labSchedulingList = labSchedulingService.select(labScheduling).stream()
                        .filter(scheduling -> scheduling.getStartWeek() <= schedulingQueryParamsDTO.getWeek()
                                && schedulingQueryParamsDTO.getWeek() <= scheduling.getEndWeek())
                        .collect(Collectors.toList());
                if (labSchedulingList.size() > 0) {
                    flag = true;
                    schedulingItemDTO.setInfo_6_7(getItem(labSchedulingList.get(0)));
                }

                labScheduling.setSession("8-9");
                labSchedulingList = labSchedulingService.select(labScheduling).stream()
                        .filter(scheduling -> scheduling.getStartWeek() <= schedulingQueryParamsDTO.getWeek()
                                && schedulingQueryParamsDTO.getWeek() <= scheduling.getEndWeek())
                        .collect(Collectors.toList());
                if (labSchedulingList.size() > 0) {
                    flag = true;
                    schedulingItemDTO.setInfo_8_9(getItem(labSchedulingList.get(0)));
                }

                labScheduling.setSession("10-12");
                labSchedulingList = labSchedulingService.select(labScheduling).stream()
                        .filter(scheduling -> scheduling.getStartWeek() <= schedulingQueryParamsDTO.getWeek()
                                && schedulingQueryParamsDTO.getWeek() <= scheduling.getEndWeek())
                        .collect(Collectors.toList());
                if (labSchedulingList.size() > 0) {
                    flag = true;
                    schedulingItemDTO.setInfo_10_12(getItem(labSchedulingList.get(0)));
                }

                labScheduling.setSession("13-15");
                labSchedulingList = labSchedulingService.select(labScheduling).stream()
                        .filter(scheduling -> scheduling.getStartWeek() <= schedulingQueryParamsDTO.getWeek()
                                && schedulingQueryParamsDTO.getWeek() <= scheduling.getEndWeek())
                        .collect(Collectors.toList());
                if (labSchedulingList.size() > 0) {
                    flag = true;
                    schedulingItemDTO.setInfo_13_15(getItem(labSchedulingList.get(0)));
                }

                if (flag) {
                    JwxtLab lab = labService.select(labId);
                    schedulingItemDTO.setLabNumber(lab.getLabNumber());
                    schedulingItemDTO.setLabName(lab.getLabName());
                    schedulingItemDTO.setDay(labScheduling.getDay());
                    schedulingItemDTOs.add(schedulingItemDTO);
                }
            }
        }
        return schedulingItemDTOs;
    }

}
